package edu.neu.ecommerceplatform.service.Impl;

import edu.neu.ecommerceplatform.Exception.ResourceNotFoundException;
import edu.neu.ecommerceplatform.dto.CreateAddressRequest;
import edu.neu.ecommerceplatform.dto.UpdateAddressRequest;
import edu.neu.ecommerceplatform.mapper.AddressMapper;
import edu.neu.ecommerceplatform.model.Address;
import edu.neu.ecommerceplatform.service.AddressService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {
    private static final Logger logger = LoggerFactory.getLogger(AddressServiceImpl.class);

    private final AddressMapper addressMapper;

    @Autowired
    public AddressServiceImpl(AddressMapper addressMapper) {
        this.addressMapper = addressMapper;
    }

    @Override
    public List<Address> getAllAddresses() {
        logger.info("获取全部地址信息");
        return addressMapper.findAll();
    }

    @Override
    public Address getAddressById(Integer id) {
        logger.info("通过ID获取地址，ID:{}", id);
        Address address = addressMapper.findById(id);
        if (address == null) {
            logger.warn("未找到地址，ID:{}", id);
            throw new ResourceNotFoundException("Address", "id", id);
        }
        return address;
    }

    @Override
    public List<Address> getAddressesByUserId(Integer userId) {
        logger.info("通过用户ID获取地址列表，用户ID:{}", userId);
        return addressMapper.findByUserId(userId);
    }

    @Transactional
    @Override
    public Address createAddress(CreateAddressRequest request) {
        logger.info("创建新的地址，用户ID:{}", request.getUserId());
        Address address = new Address(null,
                request.getUserId(),
                request.getReceiverName(),
                request.getReceiverPhone(),
                request.getCountry(),
                request.getProvince(),
                request.getCounty(),
                request.getDetailAddress());

        int rows = addressMapper.save(address);
        if (rows == 0) {
            logger.error("创建地址失败");
            throw new RuntimeException("创建地址失败，请稍后重试。");
        }
        logger.info("成功创建地址，ID:{}", address.getId());
        return address;
    }

    @Transactional
    @Override
    public Address updateAddress(Integer id, UpdateAddressRequest request) {
        logger.info("更新地址，ID:{}", id);
        Address address = addressMapper.findById(id);
        if (address == null) {
            logger.warn("未找到需要更新的地址，ID:{}", id);
            throw new ResourceNotFoundException("Address", "id", id);
        }

        address.setReceiverName(request.getReceiverName());
        address.setReceiverPhone(request.getReceiverPhone());
        address.setCountry(request.getCountry());
        address.setProvince(request.getProvince());
        address.setCounty(request.getCounty());
        address.setDetailAddress(request.getDetailAddress());

        int rows = addressMapper.update(address);
        if (rows == 0) {
            logger.error("更新地址失败，ID:{}", id);
            throw new RuntimeException("更新地址失败，请稍后重试。");
        }
        logger.info("成功更新地址，ID:{}", id);
        return address;
    }

    @Transactional
    @Override
    public void deleteAddress(Integer id) {
        logger.info("删除地址，ID:{}", id);
        Address address = addressMapper.findById(id);
        if (address == null) {
            logger.warn("未找到需要删除的地址，ID:{}", id);
            throw new ResourceNotFoundException("Address", "id", id);
        }
        addressMapper.deleteById(id);
        logger.info("成功删除地址，ID:{}", id);
    }
} 